package com.shixun.milktea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shixun.milktea.entity.Manager;
import com.shixun.milktea.entity.Order;
import com.shixun.milktea.entity.Orderdetail;
import com.shixun.milktea.mapper.MtMapper;
import com.shixun.milktea.mapper.OrderMapper;
import com.shixun.milktea.mapper.OrderdetailMapper;
import com.shixun.milktea.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.shixun.milktea.vo.*;

import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zrs
 * @since 2022-06-07
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderdetailMapper orderdetailMapper;
    @Autowired
    private MtMapper mtMapper;

    @Override
    public Integer updateOrder(Order order) {
        return orderMapper.updateOrderById(order);
    }

    @Override
    public List<Order> getAllOrderAndDetails() {
        return orderMapper.findAllOrderAndDetails();
    }


    @Override
    public PageInfo getOrderPage(Integer pageNo,Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<Order> orderIdList = orderMapper.findAllOrderId();

        List<Order> list = new ArrayList<>();
        for(Order order:orderIdList){
            list.add(orderMapper.findAllOrderAndDetailsById(order.getOrderId()));
        }

        PageInfo<Order> orderPageInfo = new PageInfo<>(list);
        return orderPageInfo;
    }

    @Override
    public List<SaleVo> getSaleCount() {
        List<SaleVo> saleVos = orderMapper.findSaleCount();
        return saleVos;
    }

    @Override
    public List<Order> getOrderByConsumerId(Integer consumerId) {
        return orderMapper.findOrderByConsumerId(consumerId);
    }

    @Override
    public List<Order> getOrderByIdAndStatus(Integer consumerId, String orderStatus) {
        return orderMapper.findOrderByIdAndStatus(consumerId,orderStatus);
    }

    @Override
    @Transactional
    public Integer addNewOrder(OrderVo orderVo) {
        Integer res = orderMapper.createOrder(orderVo);//主键回填入实体类属性中,mapper只返回影响条数
        Integer primaryKey = orderVo.getOrderId();
        List<OrderDetailVo> detailVos = orderVo.getOrderDetailVos();
        HashMap<Integer,Integer> mtIdAndNumMap = new HashMap<>();
        for(OrderDetailVo orderDetailVo:detailVos){
            mtIdAndNumMap.put(orderDetailVo.getMtId(),orderDetailVo.getOrderdetailNum());
        }
        mtMapper.updateMtNumById(mtIdAndNumMap);
        orderdetailMapper.addAllDetailsByOrderId(primaryKey,detailVos);
        return res;
    }

    @Override
    public PageInfo getOrderPageByPhone(PhoneVo phoneVo) {
        PageHelper.startPage(phoneVo.getPageNo(),phoneVo.getPageSize());
        List<Order> list = orderMapper.findOrderByPhone(phoneVo.getPhoneNumber());
        PageInfo<Order> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @Override
    public List<MtlistVo> getGroupFiveMt() {
        List<MtlistVo> mtlistVo = orderMapper.findOrderGroupFiveMt();
        return mtlistVo;
    }



}
